package gogogo.liwushuo.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.CgLeftCheckAllAdapter_1;
import gogogo.liwushuo.bean.CgLeftHeaderBean;
import gogogo.liwushuo.listener.OnItemHeaderCheckallClickListener;
import gogogo.liwushuo.util.OKHttpUtils;

/**
 * Created by Godman_Kai on 2016/7/20.
 */
public class CgLeftCheckAll_1 extends AppCompatActivity {
    @InjectView(R.id.rv_cgleft_checkall_1)
    RecyclerView rvCategoryProgram;
    @InjectView(R.id.swip_refresh_cgleft_checkall_1)
    SwipeRefreshLayout swipRefreshCgleftCheckall1;
    private String mURL;
    private List<CgLeftHeaderBean.DataBean.ColumnsBean> mDatas;
    private CgLeftCheckAllAdapter_1 mAdapter;
    private Handler mHandler;
    private LinearLayoutManager mLinearLayoutManager;
    private int mNum = 20;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cgleft_checkall_1);
        ButterKnife.inject(this);
        mDatas = new ArrayList<>();
        mAdapter = new CgLeftCheckAllAdapter_1(this, mDatas);
        rvCategoryProgram.setAdapter(mAdapter);
        mLinearLayoutManager = new LinearLayoutManager(this);
        rvCategoryProgram.setLayoutManager(mLinearLayoutManager);
        swipRefreshCgleftCheckall1.setColorSchemeColors(Color.BLUE);
        loadDatas();
        swipRefreshCgleftCheckall1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDatas.clear();
                        loadDatas();
                        swipRefreshCgleftCheckall1.setRefreshing(false);
                    }
                }, 1500);
            }
        });
        mHandler = new Handler();
        rvCategoryProgram.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int itemCount = mLinearLayoutManager.getItemCount();
                int lastPos = mLinearLayoutManager.findLastVisibleItemPosition();
                if ((lastPos + 1 == itemCount) && (newState == RecyclerView.SCROLL_STATE_IDLE)) {
                    if (mNum <= 80) {
                        loadBottomDatas(Integer.toString(mNum));
                        mNum = mNum +20;
                    }

                }
            }
        });

        setItemListener();
    }

    private void setItemListener() {
        mAdapter.setOnItemHeaderCheckallClickListener(new OnItemHeaderCheckallClickListener() {
            @Override
            public void OnItemHeaderCheckallClick(int pos, List<CgLeftHeaderBean.DataBean.ColumnsBean> datas) {
                Intent intent = new Intent(CgLeftCheckAll_1.this,HomeTitleActivity.class);
                intent.putExtra("title",datas.get(pos).getTitle());
                intent.putExtra("id",datas.get(pos).getId());
                startActivity(intent);
            }
        });
    }

    private void loadDatas() {
        mURL = getIntent().getStringExtra("program") + "0";
        OKHttpUtils.newInstance(this).getAsyncMainData(mURL, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }
            @Override
            public void onResponse(String result) {
                CgLeftHeaderBean bean = new Gson().fromJson(result, CgLeftHeaderBean.class);
                mDatas.addAll(bean.getData().getColumns());
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private void loadBottomDatas(String num) {
        mURL = getIntent().getStringExtra("program") + num;
        OKHttpUtils.newInstance(this).getAsyncMainData(mURL, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                CgLeftHeaderBean bean = new Gson().fromJson(result, CgLeftHeaderBean.class);
                mDatas.addAll(bean.getData().getColumns());
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.iv_back_program)
    public void onClick() {
        finish();
    }
}
